package com.ytsakura.blog.web;

import com.ytsakura.blog.pojo.User;
import com.ytsakura.blog.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @GetMapping("/history")
    public String history(@PageableDefault(size = 5,sort = {"date"},direction = Sort.Direction.DESC) Pageable pageable,
                          Model model, HttpSession session, RedirectAttributes redirectAttributes){
        User user = (User) session.getAttribute("user");
        if(user!=null){
            model.addAttribute("page",historyService.findHistoryByUserId(pageable,user));
        }else{
            redirectAttributes.addFlashAttribute("message","请先登录");
            return "redirect:/login";
        }
        return "history";
    }

}
